package com.hejie.videoservice.entity;

import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "videos", indexes = {
        @Index(name = "idx_user_id", columnList = "user_id"),
        @Index(name = "idx_category_id", columnList = "category_id"),
        @Index(name = "idx_status", columnList = "status"),
        @Index(name = "idx_create_time", columnList = "create_time")
})
@Data
@EntityListeners(AuditingEntityListener.class)
public class Video {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private Long userId;

    @Column(nullable = false, length = 200)
    private String title;

    @Column(columnDefinition = "TEXT")
    private String description;

    @Column(nullable = false)
    private String videoUrl;

    private String thumbnailUrl;

    private Integer duration;

    @Column(nullable = false)
    private Long categoryId;

    private String categoryName;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(
            name = "video_tags",
            joinColumns = @JoinColumn(name = "video_id")
    )
    @Column(name = "tag")
    private Set<String> tags = new HashSet<>();

    private Long viewCount = 0L;

    private Long likeCount = 0L;

    private Long commentCount = 0L;

    private Long favoriteCount = 0L;

    @Enumerated(EnumType.STRING)
    private VideoStatus status = VideoStatus.DRAFT;

    @Enumerated(EnumType.STRING)
    private Visibility visibility = Visibility.PUBLIC;

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createTime;

    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updateTime;

    @Column(nullable = false)
    private boolean deleted = false;

    public enum VideoStatus {
        DRAFT, PROCESSING, PUBLISHED, REJECTED
    }

    public enum Visibility {
        PUBLIC, PRIVATE, FOLLOWERS_ONLY
    }
}